package com.eigaland.change_machine.customview

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.appcompat.widget.AppCompatImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import com.eigaland.change_machine.R
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.model.PositionData

public class CustomViewPagerIndicator(context: Context?, attrs: AttributeSet?) : LinearLayout(context, attrs),ViewPager.OnPageChangeListener{



    public fun setData(maxSize:Int,selectIndex:Int){
        if(maxSize == 1) return

        removeAllViews()

        for(index in 0 until maxSize) {

            var imageView = AppCompatImageView(context)

            imageView.setImageResource(if(selectIndex == index)R.mipmap.ic_indicator_yellow else R.mipmap.ic_indicator_brown)

            addView(imageView)
        }
    }
    public fun setDataV(maxSize:Int,selectIndex:Int){
        if(maxSize == 1) return

        removeAllViews()

        for(index in 0 until maxSize) {

            var imageView = AppCompatImageView(context)

            imageView.setImageResource(if(selectIndex == index)R.mipmap.ic_indicator_yellow_v else R.mipmap.ic_indicator_gray_v)

            addView(imageView)
        }
    }

    fun setUpViewPager(viewPage:ViewPager){
        viewPage.addOnPageChangeListener(this)
    }


    fun setupRecycleView(recycleView: RecyclerView){
        recycleView.addOnScrollListener(object: RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                var manager = recycleView.layoutManager as LinearLayoutManager
                var position = manager.findFirstCompletelyVisibleItemPosition()

                if(position % 5 ==0) {
                        var cup = position / 5
                    for (index in 0 until childCount) {
                        var imageView = getChildAt(index) as AppCompatImageView
                        imageView.setImageResource(if (cup == index) R.mipmap.ic_indicator_yellow_v else R.mipmap.ic_indicator_gray_v)

                    }
                }
            }
        })

    }

    override fun onPageScrollStateChanged(state: Int) {

    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

    }

    override fun onPageSelected(position: Int) {
        for(index in 0 until childCount) {
            var imageView = getChildAt(index) as AppCompatImageView
            imageView.setImageResource(if(position == index)R.mipmap.ic_indicator_yellow else R.mipmap.ic_indicator_brown)

        }
    }



}